{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9a16e97f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\envs\\py8\\lib\\site-packages\\mpl_finance.py:16: DeprecationWarning: \n",
      "\n",
      "  =================================================================\n",
      "\n",
      "   WARNING: `mpl_finance` is deprecated:\n",
      "\n",
      "    Please use `mplfinance` instead (no hyphen, no underscore).\n",
      "\n",
      "    To install: `pip install --upgrade mplfinance` \n",
      "\n",
      "   For more information, see: https://pypi.org/project/mplfinance/\n",
      "\n",
      "  =================================================================\n",
      "\n",
      "  __warnings.warn('\\n\\n  ================================================================='+\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import mpl_finance as mpf\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import talib as tl\n",
    "from datetime import datetime\n",
    "\n",
    "# df=pd.read_csv(\".\\\\data\\\\tushare1_159915.csv\",encoding='utf-8') #注意中文格式\n",
    "monSum=10000\n",
    "# df.set_index('date',inplace=True)\n",
    "# df1 = df['2013-01-30':'2021-04-10']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "928c2236",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(df1.head())\n",
    "print(df1.loc['2013-01-30','close'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82c5705b",
   "metadata": {},
   "outputs": [],
   "source": [
    "monSum =10000\n",
    "close_mon=df1.loc['2013-01-30','close']\n",
    "fr =math.floor((monSum/2/close_mon)/100)*100\n",
    "print(fr)\n",
    "monSy = monSum - fr*close_mon*1.001\n",
    "print(monSy)\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "32fcf065",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20.672099999999997\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-59-fdcf188e45ba>:97: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df1['monysy']=monysyArr;\n",
      "<ipython-input-59-fdcf188e45ba>:98: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df1['monySum']=monyArr;\n",
      "<ipython-input-59-fdcf188e45ba>:99: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df1['fr']=frArr;\n",
      "<ipython-input-59-fdcf188e45ba>:100: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df1['rate']=rateArr;\n"
     ]
    }
   ],
   "source": [
    "df=pd.read_csv(\".\\\\data\\\\tushare\\\\512500_中证500.csv\",encoding='utf-8') #注意中文格式\n",
    "df.set_index('date',inplace=True)\n",
    "df1 = df['2020-04-1':'2021-04-01']\n",
    "monSum =10000\n",
    "count=0\n",
    "mon=0.0\n",
    "monSy =0\n",
    "def mc(row,fr,shou=1000):\n",
    "    global count,mon\n",
    "    global monSy\n",
    "    if fr<= 0 :\n",
    "        return fr\n",
    "    close_mon =row.low\n",
    "    fr_j =math.floor((shou/close_mon)/100);\n",
    "#     fr_j =shou*200;\n",
    "    if fr -fr_j <0:\n",
    "        return fr\n",
    "    fr= fr-fr_j;\n",
    "#     print(fr_j,fr)\n",
    "    monSy=monSy+fr_j*close_mon*0.999*100\n",
    "    count=count+1;\n",
    "    mon =mon+fr_j*close_mon*0.001*100\n",
    "#     print('-',monSy,fr_j,fr)\n",
    "    return fr;\n",
    "def mr(row,fr,shou=1000):\n",
    "    global count,mon\n",
    "    global monSy\n",
    "    close_mon =row.low\n",
    "    fr_add =math.floor((shou/close_mon)/100);\n",
    "    if monSy-fr_add*close_mon*1.001*100 <0:\n",
    "        return fr\n",
    "    fr=fr+fr_add;\n",
    "#     print('+',monSy,fr_add,fr)\n",
    "    monSy=monSy-fr_add*close_mon*1.001*100\n",
    "#     print(monSy)\n",
    "    count=count+1;\n",
    "    mon =mon+fr_add*close_mon*0.001*100\n",
    "    return fr;\n",
    "jyj = 0 # 上一次交易价\n",
    "zt = 0\n",
    "def cl(df1):\n",
    "    global count,monSum,mon,monSy,jyj,zt\n",
    "    close_mon=df1['close'].get(0)\n",
    "    fr =math.floor((monSum/2/close_mon)/100)\n",
    "    monSy = monSum - fr*close_mon*1.001*100\n",
    "    zt = close_mon\n",
    "    monysyArr=[]\n",
    "    monyArr=[]\n",
    "    frArr=[]\n",
    "    rateArr=[]\n",
    "    j = 0;\n",
    "    for i,row in df1.iterrows():\n",
    "        r = ((row.high+row.low)/2-row.sma_150)/row.sma_150\n",
    "        monysyArr.append(monSy)\n",
    "        monyArr.append(monSy+fr*close_mon*100)\n",
    "        frArr.append(fr)\n",
    "        rateArr.append(r*100)\n",
    "        j=j+1\n",
    "        close_mon =row.low\n",
    "        sum_ = monSy+fr*close_mon*100\n",
    "        rx = (close_mon-jyj)/close_mon\n",
    "        rr = (close_mon-zt)/close_mon \n",
    "        zt = close_mon        \n",
    "#         if rr>0.03:\n",
    "# #             print (rr)\n",
    "#             fr = mc(row,fr,10000*rr)\n",
    "#         if rr <-0.03:\n",
    "# #             print (rr)\n",
    "#             fr = mr(row,fr,10000*rr*-1)\n",
    "        if j%5 !=0:\n",
    "             continue;\n",
    "        if(rx>0.040 or rx<-0.03):\n",
    "            jyj = close_mon\n",
    "            if rx>0.030:\n",
    "                if r>0:\n",
    "                    fr = mc(row,fr,10000*rx)\n",
    "                if r>0.1:\n",
    "                    fr = mc(row,fr,10000*rx)\n",
    "                if r>0.2:\n",
    "                    fr = mc(row,fr,10000*rx)\n",
    "                if r>0.3:\n",
    "                    fr = mc(row,fr,10000*rx)\n",
    "            if rx<-0.02:\n",
    "                if r<0:\n",
    "                    fr = mr(row,fr,10000*rx*-1)\n",
    "                if r<-0.1:\n",
    "                    fr = mr(row,fr,10000*rx*-1)\n",
    "                if r<-0.2:\n",
    "                    fr = mr(row,fr,10000*rx*-1)\n",
    "                if r<-0.3:\n",
    "                    fr = mr(row,fr,10000*rx*-1)\n",
    "    #     print(r)\n",
    "    # print(monysyArr)\n",
    "    # print(monyArr)\n",
    "    # print(frArr)\n",
    "    # print(rateArr)\n",
    "    df1['monysy']=monysyArr;\n",
    "    df1['monySum']=monyArr;\n",
    "    df1['fr']=frArr;\n",
    "    df1['rate']=rateArr;\n",
    "    # print(df1)\n",
    "    df1.to_csv(\".\\\\data\\\\exel\\\\512500_中证500-150(count=\"+str(count)+\")\"+str(mon)+\".csv\", index=False)\n",
    "    print(mon)\n",
    "\n",
    "cl(df1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e7a2af2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab98145f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py8",
   "language": "python",
   "name": "py8"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
